package com.shr.code_interview;

public class Solution_16 {
    public static double Power(double base, int exponent) {
    	if(exponent > 0)
    		return calPower(base, exponent);
    	else return 1.0 / calPower(base, Math.abs(exponent)) ;
    }

	private static double calPower(double base, int exponent) {
		if(exponent == 0)
        	return 1;
        else if(exponent == 1)
        	return base;
        else {
        	double results = Power(base, exponent >> 1) * Power(base, exponent >> 1) ;
        	if(exponent % 2 == 1)
        		results *= base ;
        	return results ;
        }
	}
	
	public static void main(String[] args) {
		System.out.println(Power(-2, -3));
	}
}
